package backend.search.manager;

import java.util.*;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import core.profile.model.BasicProfile;
import core.util.HibernateCoreUtil;

public class UserSearchManager extends HibernateCoreUtil {

	public List<BasicProfile> getSimpleSearchResult(String searchById,String searchByName)
	{
		List<BasicProfile> lstUser = new ArrayList<BasicProfile>();
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		BasicProfile user = null ;
		Iterator<BasicProfile> userItr;
		try 
		{
				transaction = session.beginTransaction();
				//from Category c where c.name='"+name+"' and c.path='"+path+"'")
				if((searchByName!=null && searchById!=null) && (searchByName.trim().length()!=0 && searchById.trim().trim().length()!=0))
				{
					userItr = session.createQuery("from BasicProfile bc where ((bc.emailid like '%"+searchById+"%') and (bc.firstname like ''%"+searchByName+"'))").list().iterator();
				}
				else if(searchById==null || searchById.trim().length()==0)
				{
					userItr = session.createQuery("from BasicProfile bc where bc.firstname like '%"+searchByName+"%'").list().iterator();
				}
				else{
					userItr = session.createQuery("from BasicProfile bc where bc.companyname = '"+searchById+"'").list().iterator();
					
				}
				while(userItr.hasNext())
				{
					user = userItr.next() ;
					lstUser.add(user);
				}
				transaction.commit();
		}
		catch (HibernateException e) 
		{
			// TODO: handle exception
			transaction.rollback();
			e.printStackTrace();
		}
		finally 
		{
			session.close();
		}
		return lstUser;
	}
}
